package com.health.manage.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.health.manage.mapper.MedicalMapper;
import com.health.manage.mapper.UserMapper;
import com.health.manage.pojo.dto.MedicalDto;
import com.health.manage.pojo.entity.Medical;
import com.health.manage.pojo.entity.User;
import com.health.manage.service.IMedicalService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


/**
 *
 * @Description: 就诊记录业务处理
 * @author: medical
 */
@Service
public class MedicalServiceImpl extends ServiceImpl<MedicalMapper, Medical> implements IMedicalService {

	@Autowired(required = false)
	private MedicalMapper medicalMapper;
	@Autowired(required = false)
	private UserMapper userMapper;

	@Override
	public Boolean addData(Medical medical) {
		int result = this.medicalMapper.insert(medical);
		if(result > 1){
			return true;
		}else{
			return false;
		}
	}

	@Override
	public Boolean updateData(Medical medical) {
		int result = this.medicalMapper.updateById(medical);
		if(result > 1){
			return true;
		}else{
			return false;
		}
	}


	@Override
	public Boolean deleteData(Long id) {
		int result = this.medicalMapper.deleteById(id);
		if(result > 1){
			return true;
		}else{
			return false;
		}
	}

	@Override
	public Page<Medical> getPageData(MedicalDto medicalDto) {

		Page<Medical> page = new Page<>(medicalDto.getCurrentPage(), medicalDto.getPageSize());
		QueryWrapper<Medical> queryWrapper = new QueryWrapper<>();
		if(medicalDto.getUserId()!=null && !"".equals(medicalDto.getUserId())){
			queryWrapper.eq("user_id", medicalDto.getUserId());
		}
		if(medicalDto.getInfo()!=null && !"".equals(medicalDto.getInfo())){
			queryWrapper.like("info", medicalDto.getInfo());
		}
		if(medicalDto.getAccount()!=null && !"".equals(medicalDto.getAccount())){
			queryWrapper.apply("user_id in(select id from user where account like {0} )", "%" + medicalDto.getAccount() + "%");
		}
		Page<Medical>records = this.medicalMapper.selectPage(page, queryWrapper);
		for(Medical medical : records.getRecords()){
			User user = this.userMapper.selectById(medical.getUserId());
			if(user != null){
				medical.setUser(user);
			}
			User creator = this.userMapper.selectById(medical.getCreateId());
			if(creator != null){
				medical.setCreator(creator);
			}
		}
		return records;
	}


}
